﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace Weever.JKScroller
{
    public partial class MainForm : Form
    {
        readonly Keys [] _hotKeys = new Keys[] {Keys.J, Keys.K, Keys.Up, Keys.Down, Keys.H, Keys.Left, Keys.Right, Keys.A, Keys.Q};

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            foreach (Keys key in _hotKeys)
            {
                int k = (int) key;
                if (WinUser.RegisterHotKey(Handle, k, WinUser.MOD_WIN, k) == 0)
                {
                    Debug.WriteLine("RegisterHotKey failed");
                }
            }
            notifyIcon.Visible = true;
            Visible = false;
        }


        protected override void WndProc(ref Message m)
        { 
            switch (m.Msg)
            {
                case WinUser.WM_HOTKEY:
                    Keys key = (Keys)m.WParam.ToInt32();
                    OnKeyDown(key);
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }

        private static void OnKeyDown(Keys key)
        {
            Point p = MousePosition;
            IntPtr window = WinUser.WindowFromPoint(p);

            int direction;

            switch (key)
            {
                case Keys.J:
                case Keys.Down:
                case Keys.A:
                    direction = -7864320;
                    WinUser.SendMessage(window, WinUser.WM_MOUSEWHEEL, (IntPtr)(direction), WinUser.PointToLParam(p));
                    break;
                case Keys.K:
                case Keys.Up:
                case Keys.Q:
                    direction = 7864320;
                    WinUser.SendMessage(window, WinUser.WM_MOUSEWHEEL, (IntPtr) (direction), WinUser.PointToLParam(p));
                    break;
                case Keys.Left:
                    direction = -7864320;
                    WinUser.SendMessage(window, WinUser.WM_MOUSEHWHEEL, (IntPtr)(direction), WinUser.PointToLParam(p));
                    break;
                case Keys.Right:
                    direction = 7864320;
                    WinUser.SendMessage(window, WinUser.WM_MOUSEHWHEEL, (IntPtr)(direction), WinUser.PointToLParam(p));
                    break;
                case Keys.H:
                    WinUser.SendMessage(window, WinUser.WM_MBUTTONDOWN, IntPtr.Zero, WinUser.PointToLParam(p));
                    WinUser.SendMessage(window, WinUser.WM_MBUTTONUP, IntPtr.Zero, WinUser.PointToLParam(p));
                    break;
                
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (Keys key in _hotKeys)
                WinUser.UnregisterHotKey(Handle, (int)key);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Special limited edition for Dmitry Gusev (private build)", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}